<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {
	//构造函数
    function __construct(){
        $this -> need_login = true;  //控制需要登录
        parent::__construct();
        $this->load->model('User_model');
    }
    public function index(){
		$data['user']=$this->User_model->userlist();
		// var_dump($data);exit;
        $this->load->view('home/index',$data);
    }    
    public function send(){
        $this->load->helper('wechat');
        $openid=$this->input->post('openid');
        $message=$this->input->post('message');
        // 发送模板消息
		$token=gettoken('wx884248916dd13f5b','f3f4948509add2d1c1fa37ead37acf1b');
		$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$token";
		$data='{
		           "touser":"'.$openid.'",
		           "template_id":"rmUHtvkHYO6wLnGm9n21qDuAMTFEJKRGnjrpyMRn-I4",
		           "url":"http://www.homejia.club",  
		           "data":{
		                   "first": {
		                       "value":"恭喜你购买成功！",
		                       "color":"#173177"
		                   },
		                   "keyword1":{
		                       "value":"巧克力",
		                       "color":"#173177"
		                   },
		                   "keyword2": {
		                       "value":"39.8元",
		                       "color":"#173177"
		                   },
		                   "remark":{
		                       "value":"欢迎再次购买！德玛                西亚呀呀呀呀呀呀呀           呀呀呀呀呀呀呀呀嘿嘿嘿嘿嘿",
		                       "color":"#173177"
		                   }
		           }
		       }';
		$res=createMenu($data,$url);
		var_dump($res);exit;
        if ($res) {
        	echo 0;
        }else{
        	echo 1;
        }
		// var_dump($data);exit;
    }
	public function update()
	{
        $this->load->helper('wechat');
        //返回信息
        $success=0;
		$token=gettoken('wx884248916dd13f5b','f3f4948509add2d1c1fa37ead37acf1b');
		$url="https://api.weixin.qq.com/cgi-bin/user/get?access_token=$token";
		$res_openid=getCurl($url);
		$res1=json_decode($res_openid,true);
		$openids=$res1['data']['openid'];
		foreach ($openids as $key => $value) {
			//获取用户基本信息
			$url_user="https://api.weixin.qq.com/cgi-bin/user/info?access_token=$token&openid=$value&lang=zh_CH";
			$one_user=getCurl($url_user);
			$one_user=json_decode($one_user,true);
			if ($one_user) {
				$one_user['tagid_list']=json_encode($one_user['tagid_list']);
			}
			$result=$this->User_model->update_user($one_user);
			if ($result) {
				$success++;
			}
		}
		echo $success;
	}	
}
